package mrusanov.tournament

import mrusanov.team.{Team, Club}

class RoundRobinTournamentResult(val team:Team, games:List[MatchInfo]) extends Ordered[RoundRobinTournamentResult] {
  require(games.forall(_.tookPart(team)),
    "There are matches that are not related to club!")
  private val finishedGames = games filter (_.isOver)
  def victories = finishedGames.count(_.winner == team)
  def gamesCount = games.size
  def defeats = finishedGames.count(_.loser == team)

  def difference:Int = {
    var difference = 0
    finishedGames.foreach(difference += _.difference(team))
    difference
  }

  override def compare(that: RoundRobinTournamentResult): Int = {
    if (this.victories > that.victories) {
      return 1
    } else if (that.victories > this.victories) {
      return -1
    }

    if (this.difference > that.difference) {
      1
    } else if (that.difference > this.difference) {
      -1
    } else {
      0
    }
  }
}
